import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';

import {ProductInfoPage} from './ProductInfo/ProductInfo';

//发送http请求
import {Http,Headers} from '@angular/http';

import {servicess} from '../../provider/service'
import {interfaceUrls} from '../../provider/serverUrls';	//没有真实数据暂时不需要引入接口

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {
	items = [];
	param ={};
	param2 = { 
		id: ""
	}
	pInfo = {};
	//下拉刷新
	items2 = [];
	doRefresh(refresher) {
		console.log('Begin async operation', refresher);
		this.http
            .post(interfaceUrls.repertoryList, JSON.stringify(this.param), {headers: this.headers})
            .timeout(2000)
            .toPromise()
            .then(response =>{
				console.info(response.json().infoData.items)
				this.items = response.json().infoData.items;
				refresher.complete();	//刷新完成
			})
            .catch(
				err => {
					console.log('err');
				}
			);
	}
	//上拉加载

	// items3 = [];

	// doInfinite(infiniteScroll) {
	// 	console.log('Begin async operation');
	// 	this.storageService.outList()
     //        .then(data => {
	// 			console.log(data);
	// 			if (data[0].resultStatus == 'success') {
	// 				this.myItems = this.myItems.concat(data[0].infoData.items);
	// 				infiniteScroll.complete();
	// 			}
	// 		}, err => {
	// 			console.log('错误', err);
	// 		})
     //        .catch(fail => {
	// 			console.log('异常', fail);
	// 		});
	// }








	
//http请求
private headers = new Headers({'Content-Type':'application/json;chartset=utf-8'});
//url:string = '../assets/data/list.json';
constructor(public navCtrl: NavController,
						public http:Http,
						public service:servicess,
						) {

	}	
goProductInfo() {
    this.navCtrl.push(ProductInfoPage);
}
handleError(err){
console.info(err)
}


ngAfterViewInit(){	 
      this.http
      .post(interfaceUrls.repertoryList, JSON.stringify(this.param), {headers: this.headers})
      .timeout(2000) 	
      .toPromise()
      .then(response =>{ 
      	 console.info(response.json().infoData.items)
	    	 this.items = response.json().infoData.items;
      })
      .catch(
      	err => {
	    		console.log('err');
	  			}
      );
      //获取个人信息 
       this.http
      .post(interfaceUrls.personInfo, JSON.stringify(this.param2), {headers: this.headers})
      .timeout(2000) 	
      .toPromise()
      .then(response =>{ 
      	 console.info(response.json().infoData)
	    	 this.pInfo = response.json().infoData;
      })
      .catch(
      	err => {
	    		console.log('err');
	  			}
      );
      
      
      
   }
}

